<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品营养成分信息
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/goods/goodsNutrition.class.php');

class ajax_goodsNutrition {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1:
                    $this->goodsNutritionSearch();  //商品营养成分信息查询
                    break;
                case 2:
                    $this->editgoodsNutrition();    //获得要编辑的信息
                    break;
                case 3:
                    $this->searchsort();        //获得最大的序号
                    break;
                case 4:
                    $this->goodsBasicAdd();     //保存基本信息维度
                    break;
                case 5:
                    $this->goodsBasicDel();     //商品营养的删除
                    break;
                case 6:
                    $this->goodsNutritionChange();  //商品营养成分信息状态的修改
                    break;
                case 7:
					$this->searchNutri();
					break;    
                default:
                    backendError();
            }
        } else {
            backendError();
        }

    }

    /**
     * 商品营养成分信息查询
     */
    private function goodsNutritionSearch() {

        global $G_PGLOB;

        $goodsNutrition = new goodsNutrition($G_PGLOB->DBCA);
        $goodsNutritionInfo = $goodsNutrition->searchgoodsNutrition($_REQUEST);

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

        if ($goodsNutritionInfo) {

            foreach($goodsNutritionInfo as &$s_l) {

                $goods_name = ' ';
                $goods_npct = ' ';

                foreach($s_l['goods_basic'] as $k=>&$v) {
                    $goods_name .= $v['name'] . '　';
                    $goods_npct .= $v['npct'] . '　';
                }

                $edit_permission = $G_PGLOB->has_permission(541);
                if($edit_permission){
                    $result_array['rows'][] = array(
                        'id'   => $s_l['nGroupID'],
                        'cell' => array(
                            '<span><a href="javascript: void(0);" onclick="goodsNutrition.edit(' . $s_l['nGroupID'] . ')">维护值</a></span>' ,
                            $s_l['sortOrder'],
                            $s_l['nGroupName'],
                            $goods_name,
                            $goods_npct,
                            $s_l['useFlg'] == 1 ?
                                '<span><a href="javascript: void(0);" onclick="goodsNutrition.doAction(' . $s_l['nGroupID'] . ', 0) " >启用</a></span>' :
                                '<span><a href="javascript: void(0);" onclick="goodsNutrition.doAction(' . $s_l['nGroupID'] . ', 1 ) " >禁用</a></span>'
                        )
                    );
                }else{
                    $result_array['rows'][] = array(
                        'id'   => $s_l['fitName'],
                        'cell' => array(
                            $s_l['sortOrder'] ,
                            $s_l['nGroupName'],
                            $s_l['sortOrder'],
                            $goods_name,
                            $goods_npct,
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )
                    );
                }

            }
        }

        echo json_encode( $result_array );

    }

    /**
     * 待编辑信息
     */
    private  function editgoodsNutrition(){

        global $G_PGLOB;

        if (!isset($_REQUEST['nGroupID']) || !is_numeric($_REQUEST['nGroupID'])) {
            return array( 'code' => 400 , 'message' => '营养成分信息组ID不存在' );
        }

        $arg_array = array('nGroupID' => $_REQUEST['nGroupID']);
        $goodsNutrition = new goodsNutrition( $G_PGLOB->DBCA ) ;
        $result_data  = $goodsNutrition->searchgoodsNutrition( $arg_array ) ;
        
        if ($result_data) {
        	foreach ($result_data[0]['goods_basic'] as $k=>&$v) {
        		if (empty($v['npct'])) {
        			$v['npct'] = '&nbsp;';
        		}
        	}
        }

        $result = array(
            'nGroupID'	            => $result_data[0]['nGroupID'],
            'nGroupName'	        => $result_data[0]['nGroupName'],
            'useFlg'	            => $result_data[0]['useFlg'],
            'sortOrder'	        => $result_data[0]['sortOrder'],
            'goods_basic'         => $result_data[0]['goods_basic']
        );

        echo json_encode($result);
    }

    /**
     * 最大序列号查询
     */
    private function searchsort(){

        global $G_PGLOB;

        $goodsNutrition = new goodsNutrition( $G_PGLOB->DBCA ) ;

        $sortOrder = $goodsNutrition->searchSort();

        echo json_encode(array('sortOrder' => $sortOrder));
    }

    /**
     * 保存商品营养成分
     */
    private function goodsBasicAdd() {

        global $G_PGLOB;

        $goodsNutrition = new goodsNutrition($G_PGLOB->DBCA);
        $retArr = $goodsNutrition->savegoodsBasic($_REQUEST);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'],'message' => $retArr['message'] )));
        } else {
            echo json_encode($retArr);
        }
    }

    /**
     * 商品基本信息维度的删除
     */
    private function goodsBasicDel() {

        global $G_PGLOB;

        //接受要删除的参数
        $basicGroupID = $_REQUEST['p_array'];

        $goodsNutrition = new goodsNutrition($G_PGLOB->DBCA);
        $retArr = $goodsNutrition->delgoodsBasic($basicGroupID);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品营养成分信息删除成功' )));
        }

    }

    /**
     * 商品营养成分信息的修改
     */
    private function goodsNutritionChange() {

        global $G_PGLOB;

        $nGroupID  = $_REQUEST['nGroupID'];
        $useFlg        = $_REQUEST['useFlg'];

        $goodsNutrition = new goodsNutrition($G_PGLOB->DBCA);
        $retArr = $goodsNutrition->changeUseFlg($nGroupID,$useFlg);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品营养成分信息使用状态修改成功' )));
        }

    }
    
    /**
	 * 批量添加营养成分信息
	 *
	 */
	public function searchNutri(){
		global $G_SHOP;
		$nutriHandler = new goodsNutrition( $G_SHOP->DBCA ) ;
		$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$result_array = $nutriHandler->searchNutri( $_REQUEST ) ;
		
    	if ($result_array['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('nutri_result', $result_array['data']);
	    	$nutriTr = $G_SHOP->smarty->fetch('goods/nutrifetch.tpl');
	    	echo json_encode(array('code' => 200, 'data' => $nutriTr));
    	} else {
    		echo json_encode($result_array);
    	}
	}
}